package com.wangsd.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.HmacUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Set;

/**
 * @Author:qc
 * @Description:MD5加密工具类
 */
@Slf4j
public class MD5Utils {


    public static String getMD5Sign(Map<String, String> map, String key, int type) {
        StringBuffer sb = new StringBuffer();
        Set<Map.Entry<String, String>> entry = map.entrySet();
        String signStr = null;
        // &  直接加key 小写
        if (type == 1) { //kj_pay,ja_pay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb = sb.replace(sb.length() - 1, sb.length(), "");
            sb.append(key);
            signStr = DigestUtils.md5Hex(sb.toString()).toLowerCase();
        }
        //&  key=+key 大写
        else if (type == 2) { //zh_wypay,pdd_pay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append("key=" + key);
            signStr = DigestUtils.md5Hex(sb.toString());
        }
        // & key=+key 小写
        else if (type == 3) { //qf_wypay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append("key=" + key);
            signStr = DigestUtils.md5Hex(sb.toString()).toLowerCase();
        }
        // & 直接加key 大写
        else if (type == 4) { //qf_wypay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append(key);
            signStr = DigestUtils.md5Hex(sb.toString()).toUpperCase();
        } else if (type == 5) { //qf_wypay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getValue() + "|");
            }
            sb.append(key);
            signStr = DigestUtils.md5Hex(sb.toString()).toUpperCase();
        }
        log.debug("签名串：" + sb.toString() + " 签名结果：" + signStr);
        return signStr;
    }


    public static String getSHA256Sign(Map<String, String> map, String key, int type) {
        StringBuffer sb = new StringBuffer();
        Set<Map.Entry<String, String>> entry = map.entrySet();
        String signStr = null;
        // &  直接加key 小写
        if (type == 1) { //kj_pay,ja_pay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb = sb.replace(sb.length() - 1, sb.length(), "");
            sb.append(key);
            signStr = DigestUtils.sha256Hex(sb.toString()).toLowerCase();
        }
        //&  key=+key 大写
        else if (type == 2) { //zh_wypay,pdd_pay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append("key=" + key);
            signStr = HmacUtils.hmacSha256Hex(sb.toString(),key).toUpperCase();
        }
        // & key=+key 小写
        else if (type == 3) { //qf_wypay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append("key=" + key);
            signStr = DigestUtils.sha256Hex(sb.toString()).toLowerCase();
        }
        // & 直接加key 大写
        else if (type == 4) { //qf_wypay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append(key);
            signStr = DigestUtils.sha256Hex(sb.toString()).toUpperCase();
        } else if (type == 5) { //qf_wypay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getValue() + "|");
            }
            sb.append(key);
            signStr = DigestUtils.sha256Hex(sb.toString()).toUpperCase();
        }
        log.debug("签名串：" + sb.toString() + " 签名结果：" + signStr);
        return signStr;
    }


    public static String getSHA256SignPlus(Map<String, String> map, String key, int type) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
        StringBuffer sb = new StringBuffer();
        Set<Map.Entry<String, String>> entry = map.entrySet();
        String signStr = null;
        // &  直接加key 小写
        if (type == 1) { //kj_pay,ja_pay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb = sb.replace(sb.length() - 1, sb.length(), "");
            sb.append(key);
            signStr = DigestUtils.sha256Hex(sb.toString()).toLowerCase();
        }
        //&  key=+key 大写
        else if (type == 2) { //zh_wypay,pdd_pay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append("key=" + key);
//            sb = sb.replace(sb.length() - 1, sb.length(), "");
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            byte[] array = sha256_HMAC.doFinal(sb.toString().getBytes("UTF-8"));
            StringBuilder result = new StringBuilder();
            for (byte item : array) {
                result.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
            }
            System.err.println("==================="+result.toString());
            signStr = result.toString().toUpperCase();
        }
        // & key=+key 小写
        else if (type == 3) { //qf_wypay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append("key=" + key);
            signStr = DigestUtils.sha256Hex(sb.toString()).toLowerCase();
        }
        // & 直接加key 大写
        else if (type == 4) { //qf_wypay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append(key);
            signStr = DigestUtils.sha256Hex(sb.toString()).toUpperCase();
        } else if (type == 5) { //qf_wypay
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getValue() + "|");
            }
            sb.append(key);
            signStr = DigestUtils.sha256Hex(sb.toString()).toUpperCase();
        }
        log.debug("签名串：" + sb.toString() + " 签名结果：" + signStr);
        return signStr;
    }

}